package cn.cloudheritagefeign.controller;

import cn.cloudheritagefeign.service.HeritageFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/consumer") // 定义Controller的根路径
@Slf4j
public class ConsumerController {

    private final HeritageFeignClient heritageFeignClient;

    // 使用构造器注入
    @Autowired
    public ConsumerController(HeritageFeignClient heritageFeignClient) {
        this.heritageFeignClient = heritageFeignClient;
    }

    @GetMapping("/test-feign-lb")
    public ResponseEntity<String> testFeignLoadBalancer() {
        log.info("Received request to test Feign load balancer.");
        ResponseEntity<String> response = heritageFeignClient.testLoadBalancer();
        log.info("Response from inventory-warning-service: {}", response.getBody());
        return response;
    }

    // 你也可以添加一个简单的本地测试端点，确保Controller本身工作正常
    @GetMapping("/ping")
    public String ping() {
        return "Pong from ConsumerController!";
    }

    @GetMapping("/inventory/product/{id}")
    public ResponseEntity<Object> getProductById(@PathVariable("id") Long id) {
        log.info("Received request to get product by ID: {}", id);
        ResponseEntity<Object> response = heritageFeignClient.getProductById(id);
        // 根据实际情况，你可能想在这里处理 response.getBody()
        log.info("Response from inventory-warning-service for product ID {}: {}", id, response.getStatusCode());
        return response;
    }
}